function TrMat=DistApp_Hist_TrMatAssemble(TrMatCell,UnitNum)

%% Preliminary
if iscell(TrMatCell)
    if length(TrMatCell)==length(UnitNum)
        Dim     =   length(TrMatCell);
        Num     =   size(TrMatCell{1},2);
        for dd=1:Dim
            if size(TrMatCell{dd},2)~=Num
                error('Unit Transition Matrix do not have the same dimension');
            end
        end
    else
        error('Dimension mismatch!');
    end
else
    error('Transition Matrix is not inputed as cell');
end
%% Assemble the Transition Matrix
Multi   =   cumprod([1;UnitNum(1:end-1)]);
IndCell =   cell(Num,1);
ProbCell=   cell(Num,1);
for ii=1:Num
    TempIndCell     =   cell(Dim,1);
    TempProbCell    =   cell(Dim,1);
    for dd=1:Dim
        [TempRow,~,TempProb] ...
                        =   find(TrMatCell{dd}(:,ii));
        TempIndCell{dd} =   TempRow;
        TempProbCell{dd}=   TempProb;
    end
    TempInd     =   (gridmake(TempIndCell)-1)*Multi+1;
    TempProb    =   prod(gridmake(TempProbCell),2);
    IndCell{ii} =   [TempInd,repmat(ii,[length(TempInd),1])];
    ProbCell{ii}=   TempProb;
end

NZ_Ind  =   vertcat(IndCell{:});
NZ_prob =   vertcat(ProbCell{:});
TrMat   =   sparse(NZ_Ind(:,1),NZ_Ind(:,2),NZ_prob,prod(UnitNum),Num);

